{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Pandas的函数应用",
   "id": "a25d5f28c9e70c07"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:28:00.284269Z",
     "start_time": "2025-01-13T13:27:59.950188Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "# Numpy ufunc 函数，randn跟的是维数\n",
    "df = pd.DataFrame(np.random.randn(5,4) - 1) #生成5行4列的随机数据，并全部减去1\n",
    "print(df)\n",
    "\n",
    "print(np.abs(df)) #绝对值"
   ],
   "id": "57542ff641eb8794",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0 -1.520421 -0.146543 -2.813327 -0.425642\n",
      "1 -1.982209 -1.020739 -1.850440 -2.698402\n",
      "2 -1.053963 -1.662828  1.287776 -0.104119\n",
      "3 -1.505133 -3.072100  0.461567 -2.212617\n",
      "4 -1.200483 -0.913202  0.152191  0.584710\n",
      "          0         1         2         3\n",
      "0  1.520421  0.146543  2.813327  0.425642\n",
      "1  1.982209  1.020739  1.850440  2.698402\n",
      "2  1.053963  1.662828  1.287776  0.104119\n",
      "3  1.505133  3.072100  0.461567  2.212617\n",
      "4  1.200483  0.913202  0.152191  0.584710\n"
     ]
    }
   ],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:30:19.339135Z",
     "start_time": "2025-01-13T13:30:19.333994Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#apply默认作用在列上,x是每一列,因为默认axis=0\n",
    "print(df.apply(lambda x : x.max())) #每一列的最大值"
   ],
   "id": "a7bf37567652573b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -1.053963\n",
      "1   -0.146543\n",
      "2    1.287776\n",
      "3    0.584710\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:31:04.258327Z",
     "start_time": "2025-01-13T13:31:04.253342Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#apply作用在行上\n",
    "print(df.apply(lambda x : x.max(), axis=1)) #每一行的最大值"
   ],
   "id": "3fce01ccf1e32bd6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -0.146543\n",
      "1   -1.020739\n",
      "2    1.287776\n",
      "3    0.461567\n",
      "4    0.584710\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:31:56.365099Z",
     "start_time": "2025-01-13T13:31:56.357177Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用map应用到每个数据\n",
    "print(df.map(lambda x : '%.2f' % x)) #使每个数据保留两位小数\n",
    "df.dtypes"
   ],
   "id": "373d1eaecce7c4bc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2      3\n",
      "0  -1.52  -0.15  -2.81  -0.43\n",
      "1  -1.98  -1.02  -1.85  -2.70\n",
      "2  -1.05  -1.66   1.29  -0.10\n",
      "3  -1.51  -3.07   0.46  -2.21\n",
      "4  -1.20  -0.91   0.15   0.58\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0    float64\n",
       "1    float64\n",
       "2    float64\n",
       "3    float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:35:23.834771Z",
     "start_time": "2025-01-13T13:35:23.831121Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(type('%.2f' % 1.3456) ) #将数字格式化成字符串输出\n",
    "print(type(round(1.3456, 2)) ) #想保留原有float类型，使用round函数"
   ],
   "id": "857cbca62d08ce56",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'>\n",
      "<class 'float'>\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 索引排序（不重要）",
   "id": "cb3f186e3b95b57e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:43:45.524654Z",
     "start_time": "2025-01-13T13:43:45.516716Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Series\n",
    "print(np.random.randint(5, size=5))\n",
    "print('-'*50)\n",
    "s4 = pd.Series(range(10, 15), index = np.random.randint(5, size=5)) #索引随机生成\n",
    "print(s4)\n",
    "print('-'*50)\n",
    "# 索引排序,sort_index返回一个新的排好索引的series\n",
    "print(s4.sort_index())\n",
    "print(s4)\n",
    "# s4.loc[0:3]  loc索引值不唯一时直接报错\n",
    "print(s4.iloc[0:3])\n",
    "s4[0:3]  #默认用的位置索引\n",
    "# s4.iloc[0:3] #与上面那个等效"
   ],
   "id": "605b07c44a06e4c3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 3 4 4 0]\n",
      "--------------------------------------------------\n",
      "3    10\n",
      "1    11\n",
      "3    12\n",
      "3    13\n",
      "3    14\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "1    11\n",
      "3    10\n",
      "3    12\n",
      "3    13\n",
      "3    14\n",
      "dtype: int64\n",
      "3    10\n",
      "1    11\n",
      "3    12\n",
      "3    13\n",
      "3    14\n",
      "dtype: int64\n",
      "3    10\n",
      "1    11\n",
      "3    12\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "3    10\n",
       "1    11\n",
       "3    12\n",
       "dtype: int64"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "# s4.loc[1:2] #loc索引值唯一时才可以切片",
   "id": "2896eabbe93730d3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:47:20.989910Z",
     "start_time": "2025-01-13T13:47:20.981220Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame\n",
    "df4 = pd.DataFrame(np.random.randn(5, 5),\n",
    "                   index=np.random.randint(5, size=5),\n",
    "                   columns=np.random.randint(5, size=5))\n",
    "print(df4)\n",
    "#轴零是行索引排序\n",
    "df4_isort = df4.sort_index(axis=0, ascending=False) #降序排列，ascending=true升序排列\n",
    "print(df4_isort)\n",
    "\n",
    "#轴1是列索引排序\n",
    "df4_isort = df4.sort_index(axis=1, ascending=True)\n",
    "print(df4_isort)"
   ],
   "id": "1a4c1f32363a2c18",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         0         2         2         3\n",
      "0  0.405616  0.120949 -0.133672 -1.718778  1.138962\n",
      "3  1.344869 -0.049383 -1.260952 -0.654324 -0.246406\n",
      "4 -0.649788 -0.568590  0.576943 -1.267990 -0.363236\n",
      "1  0.516491  0.118112  1.393235 -0.233324 -2.166602\n",
      "3 -0.922941 -0.172678  0.395242  0.160044  1.490049\n",
      "          0         0         2         2         3\n",
      "4 -0.649788 -0.568590  0.576943 -1.267990 -0.363236\n",
      "3 -0.922941 -0.172678  0.395242  0.160044  1.490049\n",
      "3  1.344869 -0.049383 -1.260952 -0.654324 -0.246406\n",
      "1  0.516491  0.118112  1.393235 -0.233324 -2.166602\n",
      "0  0.405616  0.120949 -0.133672 -1.718778  1.138962\n",
      "          0         0         2         2         3\n",
      "0  0.405616  0.120949 -0.133672 -1.718778  1.138962\n",
      "3  1.344869 -0.049383 -1.260952 -0.654324 -0.246406\n",
      "4 -0.649788 -0.568590  0.576943 -1.267990 -0.363236\n",
      "1  0.516491  0.118112  1.393235 -0.233324 -2.166602\n",
      "3 -0.922941 -0.172678  0.395242  0.160044  1.490049\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 按值排序（在机器学习，深度学习不重要，数据分析才需要）",
   "id": "3c06ac8e22bf6c54"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:50:44.838522Z",
     "start_time": "2025-01-13T13:50:44.830141Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 按值排序,by后是column的值\n",
    "import random\n",
    "l=[random.randint(0,100) for i in range(24)] #生成24个随机数\n",
    "df4 = pd.DataFrame(np.array(l).reshape(6,4)) #生成6行4列的dataframe\n",
    "# print(df4) #查看数据,ndarray\n",
    "# print('-'*50)\n",
    "print(df4)\n",
    "print('-'*50)\n",
    "#按轴零排序，by后是列名,交换的是行\n",
    "df4_vsort = df4.sort_values(by=3,axis=0, ascending=False) #寻找的是columns里的3,重要\n",
    "print(df4_vsort)\n",
    "\n",
    "print('-'*50)\n",
    "#按轴1排序，by后行索引名，交换的是列\n",
    "df4_vsort = df4.sort_values(by=3,axis=1, ascending=False) #寻找的是index里的3\n",
    "print(df4_vsort)\n"
   ],
   "id": "f944855f861b01e7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    0   1   2   3\n",
      "0  73  32  17  69\n",
      "1  18  36  94  23\n",
      "2   2  34  46  46\n",
      "3  93  87  79   9\n",
      "4  52  78   1  49\n",
      "5  94  93  24  52\n",
      "--------------------------------------------------\n",
      "    0   1   2   3\n",
      "0  73  32  17  69\n",
      "5  94  93  24  52\n",
      "4  52  78   1  49\n",
      "2   2  34  46  46\n",
      "1  18  36  94  23\n",
      "3  93  87  79   9\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 处理缺失数据（重要）",
   "id": "fcd8af92e5912453"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T13:54:04.741112Z",
     "start_time": "2025-01-13T13:54:04.734684Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_data = pd.DataFrame([np.random.randn(3), [1., 2., np.nan],\n",
    "                       [np.nan, 4., np.nan], [1., 2., 3.]])\n",
    "print(df_data.head())\n",
    "\n",
    "#isnull来判断是否有空的数据\n",
    "print(df_data.isnull())\n",
    "\n",
    "#计算df_data缺失率\n",
    "print(df_data.isnull().sum()/len(df_data)) #计算每列的缺失率，sum（）默认是对列求和，sum(axis=1)是对行求和；len(df_data)默认是行数，len(df_data.columns)是列数 "
   ],
   "id": "297b7d5ee619d4a0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0  0.647836  0.862979 -0.436849\n",
      "1  1.000000  2.000000       NaN\n",
      "2       NaN  4.000000       NaN\n",
      "3  1.000000  2.000000  3.000000\n",
      "       0      1      2\n",
      "0  False  False  False\n",
      "1  False  False   True\n",
      "2   True  False   True\n",
      "3  False  False  False\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 删除缺失数据",
   "id": "7e98fd70e816ce27"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T14:03:36.955418Z",
     "start_time": "2025-01-13T14:03:36.949492Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#默认一个样本，任何一个特征缺失，就删除\n",
    "\n",
    "\n",
    "print(df_data.dropna(subset=[0])) #subset=[0]是指按第一列来删除,第一列有空值就删除对应的行\n",
    "# df_data\n",
    "\n",
    "#用的不多，用在某个特征缺失太多时，才会进行删除\n",
    "print(df_data.dropna(axis=1))  #某列由nan就删除该列"
   ],
   "id": "24c1fcd6ce9c3855",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2\n",
      "0  0.647836  0.862979 -0.436849\n",
      "1  1.000000  2.000000       NaN\n",
      "3  1.000000  2.000000  3.000000\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 填充缺失数据",
   "id": "dfee2616ee1f8ab9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-13T14:10:11.212387Z",
     "start_time": "2025-01-13T14:10:11.204141Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#均值，中位数，众数填充\n",
    "#给零列的空值填为-100，按特征（按列）去填充\n",
    "print(df_data.iloc[:,0].fillna(-100.))\n",
    "\n",
    "#依次拿到每一列\n",
    "for i in df_data.columns:\n",
    "    print(df_data.loc[:,i])\n",
    "    \n",
    "df_data.iloc[:,2]=df_data.iloc[:,2].fillna(df_data.iloc[:,2].mean()) #用均值填充空值\n",
    "print(df_data)\n"
   ],
   "id": "ee8ce98502020ac3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0      0.647836\n",
      "1      1.000000\n",
      "2   -100.000000\n",
      "3      1.000000\n",
      "Name: 0, dtype: float64\n",
      "0    0.647836\n",
      "1    1.000000\n",
      "2         NaN\n",
      "3    1.000000\n",
      "Name: 0, dtype: float64\n",
      "0    0.862979\n",
      "1    2.000000\n",
      "2    4.000000\n",
      "3    2.000000\n",
      "Name: 1, dtype: float64\n",
      "0   -0.436849\n",
      "1    1.281575\n",
      "2    1.281575\n",
      "3    3.000000\n",
      "Name: 2, dtype: float64\n",
      "          0         1         2\n",
      "0  0.647836  0.862979 -0.436849\n",
      "1  1.000000  2.000000  1.281575\n",
      "2       NaN  4.000000  1.281575\n",
      "3  1.000000  2.000000  3.000000\n"
     ]
    }
   ],
   "execution_count": 21
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
